package com.ruoyi.module.system.controller;

import com.gitee.deeround.jdbchelper.DbAction;
import com.gitee.deeround.jdbchelper.out.PageList;
import com.gitee.deeround.jdbchelper.resultSetExtractors.CustomApply;
import com.ruoyi.core.entity.BaseMap;
import com.ruoyi.core.entity.Output;
import com.ruoyi.flow.domain.Instance;
import com.ruoyi.flow.domain.Machine;
import com.ruoyi.flow.domain.Trans;
import com.ruoyi.flow.entity.event.FlowEvent;
import com.ruoyi.flow.entity.vo.FireVo;
import com.ruoyi.flow.service.FlowEventService;
import com.ruoyi.flow.service.FlowFireService;
import com.ruoyi.flow.service.FlowInstanceService;
import com.ruoyi.flow.service.FlowMachineService;
import com.ruoyi.web.annotations.ApiController;
import com.ruoyi.web.annotations.ApiLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@ApiController
@ApiLog
@RestController
@RequestMapping("/flow")
public class FlowController {

    @Autowired
    private DbAction dbAction;

    @Autowired
    private FlowMachineService flowMachineService;

    @Autowired
    private FlowEventService flowEventService;

    @Autowired
    private FlowFireService flowFireService;

    @Autowired
    private FlowInstanceService flowInstanceService;


    @PostMapping("/publish")
    public Output<Object> add(@RequestBody Machine entity) {

        flowMachineService.publish(entity.getMachineJson());

        return Output.success();

    }

    @PostMapping("/getMachines")
    public Output<Object> getMachines() {

        List<Machine> machines = flowMachineService.getMachines();

        return Output.success(machines);

    }

    @PostMapping("/getMachine")
    public Output<Object> getMachine(@RequestBody Machine entity) {

        Machine machine = flowMachineService.getMachine(entity.getMachineId());

        return Output.success(machine);

    }

    @PostMapping("/copyMachine")
    public Output<Object> copyMachine(@RequestBody Machine entity) {

        flowMachineService.copyMachine(entity);

        return Output.success();

    }

    @PostMapping("/getFlowTransLog")
    public Output<Object> getFlowTrans(@RequestBody Instance entity) {
        List<Trans> transList = flowInstanceService.getTransLogList(entity.getInstanceId());
        return Output.success(transList);
    }

    @PostMapping("/getFlowEvent")
    public Output<Object> getFlowEvent(@RequestBody Machine entity) {
        FlowEvent flowEvent = flowEventService.getFlowEvent(entity.getMachineId());
        return Output.success(flowEvent);
    }

    @PostMapping("/fire")
    public Output<Object> fire(@RequestBody FireVo fireVo) {
        flowFireService.fire(fireVo);
        return Output.success();
    }


    @PostMapping("/getTestData")
    public Output<Object> getTestData(@RequestBody Machine entity) {

        String machineId = entity.getMachineId();

        String sql = "select * from flow_" + machineId + " order by id desc";
        PageList<Map<String, Object>> page = dbAction.queryForPage(sql, 1, 10, new CustomApply<Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(Map<String, Object> map) {
                return new BaseMap(map);
            }
        });

        return Output.success(page.getList());
    }

}
